package com.mspbots.sync.tsheets;

import com.mspbots.sync.model.TenantConfiguration;
import lombok.Data;
import org.springframework.util.StringUtils;
import reactor.core.publisher.Mono;

import javax.validation.constraints.NotEmpty;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * At this point you have an authorization 'code' for the user of your app.
 * To exchange the code for an access token,
 * your application needs to do a POST to our /grant API end-point. In this step,
 * TSheets will check that the authorization code was issued to the same application that is making the token request.
 * <p>
 * The access_token should be included with every call to the API.
 * Failure to include the access token or using an expired token will result in a 401 response.
 * Note that when you receive your access token, you also receive the user_id and client_id properties
 * that are associated with the user that the access token is for. These properties are provided for convenience,
 * to potentially save you the need for making a request to the current user endpoint.
 * <p>
 * If the client has IP Authorization security enabled with the block all access option turned on,
 * the request could fail with a 499 status code if it comes from an unauthorized IP address.
 * All subsequent requests will also fail until the device switches to an authorized IP address.
 *
 * @author Alex bob(https://github.com/vnobo)
 * @request: https://rest.tsheets.com/api/v1/grant
 * @date Created by 2019/7/25
 */
@Data
public class SheetsProperties {

    /**
     * The integrator’s ConnectWise Application Id.
     */
    @NotEmpty
    private String token;

    /**
     * The ConnectWise Application URL.
     */
    @NotEmpty
    private String baseUrl;

    /**
     * This parameter MUST always be set to the value of
     * the OAuth client ID that you obtained when you
     * registered your app in the
     * TSheets API Add-On in your TSheets account.
     */
    @NotEmpty
    private String clientId;

    /**
     * his parameter MUST always be set to the
     * value of the OAuth client secret that you
     * obtained when you set up your app in the
     * API Add-On in your TSheets account.
     */
    @NotEmpty
    private String clientSecret;

    /**
     * Found on the tenant id.
     */
    @NotEmpty
    private Integer tenantId;

    public static Mono<SheetsProperties> build(List<TenantConfiguration> configurations) {
        if (configurations.size() > 0) {
            Map<String, String> map = configurations.stream()
                    .filter(config -> config.getKeyword().startsWith("mspbots.sync.tsheets"))
                    .filter(c -> !StringUtils.isEmpty(c.getKeywordValue()))
                    .collect(Collectors.toMap(TenantConfiguration::getKeyword, TenantConfiguration::getKeywordValue));
            if (map.size() == 0) {
                return Mono.empty();
            }

            if (map.get("mspbots.sync.tsheets.token").isEmpty()) {
                return Mono.empty();
            }

            SheetsProperties sheetsProperties = new SheetsProperties();
            sheetsProperties.setTenantId(configurations.get(0).getTenantId());
            sheetsProperties.setToken(map.get("mspbots.sync.tsheets.token"));
            sheetsProperties.setBaseUrl(map.get("mspbots.sync.tsheets.baseUrl"));
            sheetsProperties.setClientId(map.get("mspbots.sync.tsheets.clientId"));
            sheetsProperties.setClientSecret(map.get("mspbots.sync.tsheets.clientSecret"));
            return Mono.just(sheetsProperties);
        }
        return Mono.empty();
    }
}
